package modulesAPI.v2.composite;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.border.Border;

import model.Model;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModComboBox;
import modulesAPI.v2.basics.ModDoubleTextField;
import modulesAPI.v2.basics.ModTextField;
import person.Employee;
import person.Employee.Role;
import person.Person;
import transaction.Transaction;
import transaction.Transaction.Type;

import com.caterina.components.CLabel;
import com.caterina.components.CPanel;
import com.caterina.resources.CColors;
import com.caterina.resources.CWords;

public class MTransaction extends Module<Transaction> {
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di una transazione
	 * */
	public MTransaction(Module<?> ownerModule, Transaction content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		createModules();
		this.addModules(employee,type, amount, date, person, reasonForPayment);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di una transazione
	 * */
	public MTransaction(Module<?> ownerModule,String tag,String description, ViewType viewType){
		this(ownerModule, 
				new Transaction(Model.NO_ID, 
				new Person(Model.NO_ID,"","","","","","",""), 
				new Employee(Model.NO_ID, "", "", "", "", "", "", "", Role.FINANTIAL_RESPONSABLE, "", new GregorianCalendar(), 0), 
				Type.PURCHASE, 0.0, new GregorianCalendar(), ""), 
				tag, description, viewType);
	}
	private void createModules(){
		this.person=new MPerson(this, this.getContent().getBeneficiary(), "OTHER_PART", "OTHER_PART_DESC",this.getViewType());
		this.employee=new MEmployee(this, this.getContent().getEmployee(),"EMPLOYEE", "EMPLOYEE_DESC", ViewType.READ_ONLY);
		this.amount=new ModDoubleTextField(this, this.getContent().getAmount(), "TRANSACTION_AMOUNT", "TRANSACTION_AMOUNT_DESC",this.getViewType());
		this.reasonForPayment=new ModTextField(this, this.getContent().getReasonForPayment(), "REASON_FOR_PAYMENT", "REASON_FOR_PAYMENT_DESC",this.getViewType());
		this.date=new ModCalendar(this, this.getContent().getDate(), "TRANSACTION_DATE", "TRANSACTION_DATE_DESC",this.getViewType());
		this.type=new ModComboBox(this, this.getContent().getType(), "TRANSACTION_TYPE", "TRANSACTION_TYPE_DESC",this.getViewType(), Type.values());
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		this.employee.setVisible(true);
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		this.employee.setVisible(false);
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		CPanel retPanel=new CPanel(new BorderLayout());
		retPanel.add(getTopPanel((Type) type.getContent(), date.getContent(),isSelected),BorderLayout.NORTH);
		retPanel.add(getTransactionPersonPanel(employee.getContent(), isSelected),BorderLayout.WEST);
		retPanel.add(getArrowPanel((Type) type.getContent(),amount.getContent(),isSelected),BorderLayout.CENTER);
		retPanel.add(getTransactionPersonPanel(person.getContent(), isSelected),BorderLayout.EAST);
		retPanel.setBorder(
				BorderFactory.createCompoundBorder(
						BorderFactory.createMatteBorder(0, 0, 1, 0, CColors.BUTTON_PRESSED_CATERINA),
						BorderFactory.createEmptyBorder(3,3,3,3))
				);
		if(isSelected){
			retPanel.setBackground(CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
			retPanel.setForeground(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		}else{
			retPanel.setBackground(CColors.LIST_ENTRY_BACKGROUND);
			retPanel.setForeground(CColors.LIST_ENTRY_MEMO_FOREGROUND);
		}
		final int w=350,h=95;
		retPanel.setMinimumSize(new Dimension(w,h));
		retPanel.setMaximumSize(new Dimension(w,h));
		retPanel.setPreferredSize(new Dimension(w,h));
		return retPanel;
	}
	private CPanel getTopPanel(Type trType,Calendar date,boolean isSelected){
		CPanel retPanel=new CPanel();
		retPanel.setLayout(new BoxLayout(retPanel, BoxLayout.X_AXIS));
		retPanel.add(new CLabel("<html><b>"+CWords.getWord(trType.name())+"</b> - "+CWords.getFormattedDate(date)+"</html>"));	
		retPanel.setOpaque(false);
		if(isSelected){
			retPanel.setForeground(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		}else{
			retPanel.setForeground(CColors.LIST_ENTRY_MEMO_FOREGROUND);
		}	
		return retPanel;
	}
	private CPanel getArrowPanel(Type trType,double amount,boolean isSelected){
		final int w=40,h=60;
		CPanel retPanel=new CPanel(new BorderLayout());
		retPanel.setMinimumSize(new Dimension(w,h));
		retPanel.setMaximumSize(new Dimension(w,h));
		retPanel.setPreferredSize(new Dimension(w,h));
		retPanel.add(new CLabel("<html><b>"+Double.toString(amount)+" "+CWords.getWord("CURRENCY")+"</b></html>"),BorderLayout.SOUTH);
		retPanel.setOpaque(false);
		String imageUrl="graphics/transactions/Arrow_";
		if(trType==Type.PURCHASE||trType==Type.SALARIES_PAYMENT||trType==Type.RENT)
			imageUrl+="R_";
		else imageUrl+="L_";
		if(isSelected){
			imageUrl+="dark";
			retPanel.setForeground(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		}else{
			imageUrl+="light";
			retPanel.setForeground(CColors.LIST_ENTRY_MEMO_FOREGROUND);
		}
		imageUrl+=".png";
		ImageIcon tempImg=new ImageIcon(imageUrl);
		Image img=tempImg.getImage().getScaledInstance(tempImg.getIconWidth()-6, 
				tempImg.getIconHeight()-6,Image.SCALE_SMOOTH );
		ImageIcon arrowIcon=new ImageIcon(img);
		CLabel arrowLabel=new CLabel("");
		arrowLabel.setIcon(arrowIcon);
		retPanel.add(arrowLabel,BorderLayout.CENTER);
		return retPanel;
	}
	private CPanel getTransactionPersonPanel(Person person,boolean isSelected){
		final int w=130,h=55;
		CPanel retPanel=new CPanel(new BorderLayout());
		retPanel.setMinimumSize(new Dimension(w,h));
		retPanel.setMaximumSize(new Dimension(300000,h));
		retPanel.setPreferredSize(new Dimension(w,h));
		retPanel.add(new CLabel("<html><p>"+person.getName()+"</p><p>"+person.getSurname()+"</p></html>"));
		Border margin=BorderFactory.createEmptyBorder(7, 7, 7, 7);
		Border inBorder=BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(CColors.GRAY,1), margin);
		Border outBorder=null;
		if(isSelected){
			retPanel.setBackground(CColors.WHITE);
			retPanel.setForeground(CColors.LABEL_PLANE_FOREGROUND);
			outBorder=BorderFactory.createMatteBorder(5,1,5,3, CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
		}else{
			outBorder=BorderFactory.createMatteBorder(5,1,5,3, CColors.LIST_ENTRY_BACKGROUND);
			retPanel.setBackground(CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
			retPanel.setForeground(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		}
		Border collapsed=BorderFactory.createCompoundBorder(outBorder, inBorder);
		retPanel.setBorder(collapsed);
		return retPanel;
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
		this.setContent(new Transaction(
				this.getContent().getId(),
				person.getContent(),
				employee.getContent(),
				(Type)type.getContent(),
				amount.getContent(),
				date.getContent(),
				reasonForPayment.getContent()
				));
	}
	@Override
	protected void afterFireChange(Module<?> mod) {		
	}
	private MPerson person;
	private MEmployee employee;
	private ModComboBox type;
	private ModCalendar date;
	private ModTextField reasonForPayment;
	private ModDoubleTextField amount;
} 
